//
//  YZPublishCommunityContentCell.m
//  VET
//
//  Created by YZ on 2018/7/10.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPublishCommunityContentCell.h"
#import "YZPlaceholderTextView.h"
#import "YZPublishCommunityImgCell.h"
#import "YZPublishCommunityVideoCell.h"

@interface YZPublishCommunityContentCell()<UICollectionViewDelegate,UICollectionViewDataSource,UITextViewDelegate>

@property (nonatomic, strong) YZPlaceholderTextView *textAreaView;

@property (nonatomic, strong) UICollectionView *imgColloectView;

@end

@implementation YZPublishCommunityContentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.layer.masksToBounds = YES;
        [self.contentView addSubview:self.textAreaView];
        [self.contentView addSubview:self.imgColloectView];
        
        [self makeContraints];
    }
    return self;
}

- (void)makeContraints
{
    [self.textAreaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(kCurrentDeviceNum(20));
        make.height.mas_equalTo(kCurrentDeviceNum(142));
        make.right.equalTo(self.contentView.mas_right).offset(-kCurrentDeviceNum(20));
    }];
    
    [self.imgColloectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.textAreaView);
        make.top.equalTo(self.textAreaView.mas_bottom).offset(kCurrentDeviceNum(20));
        make.height.mas_equalTo((kScreenWidth-40-2*5)/3);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-kCurrentDeviceNum(20));
    }];
}

#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.imgs.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    NSObject *obj = self.model.imgs.firstObject;
    if ([obj isKindOfClass:[YZPublishCommunityImgModel class]]) {
        YZPublishCommunityImgCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YZPublishCommunityImgCell" forIndexPath:indexPath];
        cell.model = self.model.imgs[indexPath.row];
        return cell;
    }else if ([obj isKindOfClass:[YZPublishCommunityVideoModel class]])
    {
        YZPublishCommunityImgCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YZPublishCommunityVideoCell" forIndexPath:indexPath];
        cell.model = self.model.imgs[indexPath.row];
        return cell;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == self.model.imgs.count - 1 && [self.model.imgs.firstObject isKindOfClass:[YZPublishCommunityImgModel class]])
    {
        if(_delegate && [_delegate respondsToSelector:@selector(communityContentCellAddItemSelected)]){
            [_delegate communityContentCellAddItemSelected];
        }
    }else
    {
        if ([self.model.imgs.firstObject isKindOfClass:[YZPublishCommunityImgModel class]]) {
            if(_delegate && [_delegate respondsToSelector:@selector(communityContentCell:imgItemDidSelectedIndex:)]){
                [_delegate communityContentCell:[collectionView cellForItemAtIndexPath:indexPath] imgItemDidSelectedIndex:indexPath.row];
            }
        }else if([self.model.imgs.firstObject isKindOfClass:[YZPublishCommunityVideoModel class]])
        {
            if(_delegate && [_delegate respondsToSelector:@selector(communityContentCell:videoItemDidSelectedIndex:)]){
                [_delegate communityContentCell:[collectionView cellForItemAtIndexPath:indexPath] videoItemDidSelectedIndex:indexPath.row];
            }
        }
    }
}

#pragma mark -- UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView
{
    self.model.contentText = textView.text;
}


#pragma mark -- setters && getters

- (YZPlaceholderTextView *)textAreaView
{
    if(!_textAreaView){
        _textAreaView = [YZPlaceholderTextView new];
        _textAreaView.placeholder = @"说说您的日常工作、生活、感受吧";
        _textAreaView.delegate = self;
        _textAreaView.maxTextLength = 500;
        _textAreaView.font = kCurrentDeviceFont(16);
        [_textAreaView setPlaceholderColor:kColorWithHex(0x9B9B9B)];
        [_textAreaView setPlaceholderFont:kCurrentDeviceFont(16)];
    }
    return _textAreaView;
}

- (UICollectionView *)imgColloectView
{
    if(!_imgColloectView){
        UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
        CGFloat widthHeight =  (kScreenWidth-kCurrentDeviceNum(40)-2*kCurrentDeviceNum(5))/3.0;
        fl.itemSize = CGSizeMake(widthHeight, widthHeight);;
        fl.minimumLineSpacing = kCurrentDeviceNum(5);
        fl.minimumInteritemSpacing = kCurrentDeviceNum(5);
        fl.sectionInset = UIEdgeInsetsZero;
        _imgColloectView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
        [_imgColloectView registerClass:[YZPublishCommunityImgCell class] forCellWithReuseIdentifier:@"YZPublishCommunityImgCell"];
        [_imgColloectView registerClass:[YZPublishCommunityVideoCell class] forCellWithReuseIdentifier:@"YZPublishCommunityVideoCell"];
        _imgColloectView.backgroundColor = kColorWithHex(0xFFFFFF);
        _imgColloectView.dataSource = self;
        _imgColloectView.delegate = self;
    }
    return _imgColloectView;
}

- (void)setModel:(YZPublishCommunityContentModel *)model
{
    _model = model;
    [self.imgColloectView reloadData];
    self.textAreaView.text = model.contentText;
    [self.imgColloectView reloadData];
    [self.imgColloectView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(model.imgs.count/3*kCurrentDeviceNum(5)+ceil(model.imgs.count/3.0)*kCurrentDeviceNum((kScreenWidth-40-2*5)/3));
    }];
}
@end
